<template>
  <div class="departments-container">
    <el-card>
      <!-- <el-row type="flex" justify="space-around" align="middle" style="height:40px">
        <el-col>
          <span>江苏传智播客教育科技股份有限公司</span>
        </el-col>
        <el-col :span="4">
          <el-row type="flex" justify="end" align="middle">
            <el-col>
              <span>负责人</span>
            </el-col>
            <el-col>
              <el-dropdown>
                <span>
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item>添加子部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-col>
      </el-row> -->
      <tree-tools :tree-node="company" :is-root="false" @add="handleAdd" />
    </el-card>
    <el-tree
      v-loading="loading"
      :data="departs"
      :default-expand-all="true"
      :props="defaultProps"
    >
      <treeTools slot-scope="{data}" :tree-node="data" @add="handleAdd" @edit="handleEdit" @refreshList="getDepartments" />
    </el-tree>
    <addDept ref="addDept" :dialog-visible.sync="dialogVisible" :current-node="currentNode" />
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import addDept from './components/add-dept.vue'
import { getDepartmentsAPI } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'DepartmentsIndex',
  components: {
    treeTools,
    addDept
  },
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      currentNode: {},
      departs: [],
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      dialogVisible: false
    }
  },
  created() {
    addDept
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      try {
        this.loading = true
        const res = await getDepartmentsAPI()
        // console.log(res)
        this.company = { name: res.companyName, manager: '负责人', id: '' }
        this.departs = tranListToTreeData(res.depts, '')
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    },
    handleAdd(node) {
      // console.log('adadss')
      this.currentNode = node
      this.dialogVisible = true
    },
    handleEdit(node) {
      this.dialogVisible = true
      this.$refs.addDept.formData = { ...node }
      this.currentNode = node
    }
  }
}
</script>

<style lang="scss" scoped>
.departments-container{
  width: 900px;
  margin: 20px auto;
}
</style>

